package com.xmall.member.define.config;

import com.xmall.msg.queue.MemberModule;
import com.xmall.msg.queue.OrderModule;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class AmqpPrivateExchangeConfig {


    @Resource
    private RabbitAdmin rabbitAdmin;

    /**
     * 订单创建后删除购物车流程
     * 创建订单：路由键（order.create.order.key） --> 广播交换机（order.order.create.fanout.exchange）
     *                                        --> 队列（member.order.create.queue）
     */
    @Bean
    public Exchange orderCreateFanoutExchange() {
        FanoutExchange exchange = new FanoutExchange(OrderModule.OrderCreate.ORDER_CREATE_FANOUT_EXCHANGE);
        rabbitAdmin.declareExchange(exchange);
        return exchange;
    }
    @Bean
    public Queue orderCreateQueue() {
        Queue queue = new Queue(MemberModule.OrderCreate.ORDER_CREATE_QUEUE, true, false, false);
        rabbitAdmin.declareQueue(queue);
        return queue;
    }
    @Bean
    public Binding orderCreateBinding() {
        Binding binding = BindingBuilder.bind(orderCreateQueue()).to(orderCreateFanoutExchange()).with(MemberModule.OrderCreate.ORDER_CREATE_ROUTING_KEY).noargs();
        rabbitAdmin.declareBinding(binding);
        return binding;
    }
}
